<?php
include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/controller/weixinUtil.php";

$tool = new weixinTool("HPQwjnca_An3LjptuP8zdQWvxkOANDn3FhLygAHLwe0");
$token = $tool->access_token;
$userId = -1;

//    得到URI中的code值
function getCode(){
    $pos1 = strpos($_SERVER['REQUEST_URI'], "code=");
    $pos2 = strpos($_SERVER['REQUEST_URI'], "&state");
    if($pos1 != null && $pos2 != null && $pos2-$pos1 > 0){
        $temp = substr($_SERVER['REQUEST_URI'], $pos1+5, $pos2-$pos1-5);
    }
    return $temp;
}
$code = getCode();
//    获取微信企业号的用户UserId
$userInfo = $tool->get_user_info_by_code($code);
$weixinId = $userInfo['UserId'];

//根据weixinId在数据库中查询userId
$sql = "select * from user where weixinId = '{$weixinId}'";
$conn = new db();
$db = $conn->getDBConn();
$row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存

if($row){
    $userId = $row['userId'];
}

?>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <!-- meta使用viewport以确保页面可自由缩放 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/jquery-3.2.1.js"></script>

    <script type="text/javascript" src="js/bootstrap.min.js"></script>
<!--    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>-->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=xZXH3sRzkIgsZa70oqf9uDzScnSb13Il"></script>

    <title>待完成任务</title>
    <link rel="stylesheet" href="css/global.css" />
    <link rel="stylesheet" href="css/acceptOngoing.css" />

</head>
<body>

<div style="margin:100px auto;text-align: center;display:none" id="none-task-tip" >
    <img src="img/pikaqiu1.gif" style="width:83px;height:106px;"/>
    <h4> 您还没有需要完成的任务,</h4>
    <h4>快去接受任务吧！</h4>
</div>

<div class="my-container" id="accept-ongoing-list">

</div>

</body>

<script type="text/javascript">
    var accept_tasks;
    var userId;

    $(function() {
        userId = <?php echo $userId;?>;
        var action = "controller/getAcceptedTask.php";
        var data = {
            "userid":userId
        };

        $.ajax({
            type: "POST",
            url: action,
            data: {data:JSON.stringify(data)},
            success: function(response)
            {
                accept_tasks = JSON.parse(response.trim().replace(/\n/g,":"));
                if(accept_tasks.length == 0){
                    $('#none-task-tip').css('display',"block");
                } else{
                    $('#none-task-tip').css('display',"none");
                }
                for(var i=0; i<accept_tasks.length; i++){
                    $('#accept-ongoing-list').append(createListItem(accept_tasks[i]));
                }
            },
            error:function(request){
                alert("connection error");
            }
        });
    });

    ////////////////////////////////////创建列表项///////////////////////////////////////////////////////////
    function createListItem(task){
        task.userId = userId;
        var p = encodeURIComponent(JSON.stringify(task));
        var uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx751e041cdef857d6&redirect_uri=http%3a%2f%2f118.178.94.215%2facceptTaskDetail.php&response_type=code&scope=snsapi_userinfo&state="
            +  p + "#wechat_redirect";
        var s = "<div class='list-group'>" +
            "<a href='" + encodeURI(uri)  + "' class='list-group-item'>" +
            " <h4 class='list-group-item-heading'>" + task.stageDesc + "</h4>" +
            "<p class='list-group-item-text'>" +
            "<span>所属任务：</span>" +
            "<span>"+ task.taskDesc + "</span>" +
            "</p>" +
            "<p class='list-group-item-text'>" +
            "<span>目前阶段：</span>" +
            "<span>" + task.currentStage + "</span>" +
            "</p>" +
            "<p class='list-group-item-text'>" +
            "<span>契约时间：</span>" +
            "<span>" + task.contract + "</span>" +
            "</p>" +
            "</a>" +
            "</div>";
        return s;
    }


    //我接受的正在进行的任务列表
    //    accept_tasks = [
    //        {
    //            "bonusReward": 3760.42,
    //            "currentStage": 8,
    //            "ddl": 1490345137000,
    //            "reward": 8962.73,
    //            "stageId": 1,
    //            "stageDesc": "jtest_description_1490345137348",
    //            "stageName": "jtest_name_1490345137348",
    //            "taskDeadline": 1490345137000,
    //            "contract": "2017/6/21 18:30:00",
    //            "taskDesc": "取快递",
    //            "taskId": 45,
    //            "taskProgress": "8/9",
    //            "taskTitle": "取快递",
    //            "locations": [
    //                {
    //                    "address": "jtest_address_1490345137403",
    //                    "id": 67,
    //                    "latitude": 43.1964,
    //                    "longitude": -19.0508,
    //                    "stageId": 45,
    //                    "type": 0,
    //                    "inputs": [
    //                        {
    //                            "actionId": 34,
    //                            "desc": "jtest_description_1490345137592",
    //                            "id": 34,
    //                            "type": 1,
    //                            "value": "jtest_value_1490345137592"
    //                        }
    //                    ],
    //                    "outputs": [
    //                        {
    //                            "id":1,
    //                            "actionId":1,
    //                            "type":0,
    //                            "desc":"This is description",
    //                            "value":"path/content",
    //                            "isActive":true,
    //                            "intervalValue":1,
    //                            "upBound":10.0,
    //                            "lowBound":2.0,
    //                            "entries":"Yes;No",
    //                            "aggregaMethod":1
    //                        },
    //                        {
    //                            "id":1,
    //                            "actionId":1,
    //                            "type":1,
    //                            "desc":"This is description",
    //                            "value":"path/content",
    //                            "isActive":true,
    //                            "intervalValue":1,
    //                            "upBound":10.0,
    //                            "lowBound":2.0,
    //                            "entries":"Yes;No",
    //                            "aggregaMethod":1
    //                        },
    //                        {
    //                            "id":1,
    //                            "actionId":1,
    //                            "type":2,
    //                            "desc":"This is description",
    //                            "value":"path/content",
    //                            "isActive":true,
    //                            "intervalValue":1,
    //                            "upBound":10.0,
    //                            "lowBound":2.0,
    //                            "entries":"Yes;No",
    //                            "aggregaMethod":1
    //                        },
    //                        {
    //                            "id":1,
    //                            "actionId":1,
    //                            "type":3,
    //                            "desc":"This is description",
    //                            "value":"path/content",
    //                            "isActive":true,
    //                            "intervalValue":1,
    //                            "upBound":10.0,
    //                            "lowBound":2.0,
    //                            "entries":"Yes;No",
    //                            "aggregaMethod":1
    //                        }
    //                    ]
    //                },
    //                {
    //                    "id": 0,
    //                    "inputs": [],
    //                    "latitude": 0,
    //                    "longitude": 0,
    //                    "outputs": [],
    //                    "stageId": 0,
    //                    "type": -1
    //                }
    //            ]
    //        }
    //    ];


</script>



</html>




